package com.softcomputer.rpt;

import com.softcomputer.rpt.ejb.ExchangeSession;
import com.softcomputer.rpt.ejb.LogMessageSession;
import com.softcomputer.rpt.entity.ExCourse;
import com.softcomputer.rpt.entity.LogMessage;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ManagedBean(name = "manage")
@SessionScoped
public class OperationManagementBean implements Serializable {

   @EJB
   private ExchangeSession exCourseSession;
   @EJB
   private LogMessageSession logMessageSession;
   private LogMessage fromAmount;
   private ExCourse course;
   private List<BigDecimal> values;
   private List<String> currencies;
   private String sellCurrency;
   private String buyCurrency;
   private static final Log LOG = LogFactory.getLog(OperationManagementBean.class);

   public OperationManagementBean() {
   }

   public List<LogMessage> getLogMessages() {

      List<LogMessage> logMessages;

      LOG.info("Message list requested");
      try {
         logMessages = logMessageSession.retrieve();
      } catch (Exception exception) {
         LOG.error("Message retrievement failed" + exception.getMessage());
         return new ArrayList<LogMessage>();
      }

      LOG.info("Log messages retrieved");

      Collections.reverse(logMessages);

      return logMessages;
   }

   public void performExchange() {

      MathContext mc = new MathContext(3);

      BigDecimal amountPurchased = new BigDecimal(0, mc);

      LOG.info("Exchange operation requested");

      try {
         float exchangeRate = exCourseSession.getExchangeRateFromXml(sellCurrency, buyCurrency);

         amountPurchased = exCourseSession.exchange(fromAmount.getMessage(), exchangeRate);

      } catch (Exception exception) {
         LOG.error("Exchange operation failed: " + exception.getMessage());
      }

      SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
      Date date = new Date();

      String logMessage = dateFormat.format(date) + fromAmount.getMessage()
              + sellCurrency
              + " purchased - "
              + amountPurchased
              + buyCurrency;

      fromAmount.setMessage(logMessage);

      try {
         logMessageSession.persist(fromAmount);
      } catch (Exception exception) {
         LOG.error("Unable to save exchange operation info: " + exception.getMessage());
      }

      LOG.info("Exchanging session finished: " + logMessage);

      fromAmount = null;

   }

   public List<ExCourse> getExchangeCourses() {

      List<ExCourse> exchangeCoursesList;

      LOG.info("Exchange rate list requested");

      try {
         exchangeCoursesList = exCourseSession.retrieve();
      } catch (Exception exception) {
         LOG.error("Exchange rate list failed to retrieve: " + exception.getMessage());
         return new ArrayList<ExCourse>();
      }

      LOG.info("Exchange rate list retrieved successfully");
      return exchangeCoursesList;
   }

   public List<BigDecimal> getValues() {
      values = exCourseSession.getDollarRate();
      return values;
   }

   public ExCourse getCourse() {
      if (course == null) {
         course = new ExCourse();
      }
      return course;
   }

   public LogMessage getMessage() {
      if (fromAmount == null) {
         fromAmount = new LogMessage();
      }
      return fromAmount;
   }

   public void setMessage(String text) {
      fromAmount.setMessage(text);
   }

   public List<String> getCurrencies() {
      currencies = exCourseSession.getCurrency();
      return currencies;
   }

   public String getSellCurrency() {
      return sellCurrency;
   }

   public void setSellCurrency(String curr) {
      sellCurrency = curr;
   }

   public String getBuyCurrency() {
      return buyCurrency;
   }

   public void setBuyCurrency(String curr) {
      buyCurrency = curr;
   }

   public void setId(long id) {
      fromAmount.setId(id);
   }
}
